import { doGetMaterials, doGetInformation } from '@/api/demand/order/index';
import type { InformationOptions, MaterialsOptions } from '../Order/type';
import type { MaterialsResponse, InformationResponse } from '@/api/demand/order/model/type';

export const useGetSelectData = () => {
    const selectLoading = ref(false);

    /**
     * @: 物料编码 + 物料名称
     */
    const itemCodeOption = ref<MaterialsOptions[]>([]);
    const addItemCodeOption = ref<MaterialsResponse[]>([]);
    const itemCodeRemoteMethod = async (query: string, key: 'materialCode' | 'materialName') => {
        selectLoading.value = true;
        try {
            const { data } = await doGetMaterials({ [key]: query });
            if (data) {
                addItemCodeOption.value = data;
                const obj = {
                    label: '',
                    options: data
                };

                itemCodeOption.value[1] = obj;
            }
        } catch (error) {
            console.log('-------', error);
        }
        selectLoading.value = false;
    };

    /**
     * @: 物料编码change
     */
    const changeItemCode = (e: MaterialsResponse) => {
        itemCodeOption.value[0].options = e ? [e] : [];
    };

    /**
     * @: 客户编码 + 客户公司名称
     */
    const customerOption = ref<InformationOptions[]>([]);
    const addCustomerOption = ref<InformationResponse[]>([]);
    const customerRemoteMethod = async (
        query: string,
        key: 'customerCode' | 'customerCompanyName'
    ) => {
        selectLoading.value = true;
        try {
            const { data } = await doGetInformation({ [key]: query });
            if (data) {
                addCustomerOption.value = data;
                const obj = {
                    label: '',
                    options: data
                };

                customerOption.value[1] = obj;
            }
        } catch (error) {
            console.log('-------', error);
        }
        selectLoading.value = false;
    };

    /**
     * @: 客户编码change
     */
    const changeCustomer = (e: InformationResponse) => {
        customerOption.value[0].options = e ? [e] : [];
    };

    return {
        customerRemoteMethod,
        itemCodeRemoteMethod,
        customerOption,
        itemCodeOption,
        changeItemCode,
        changeCustomer,
        selectLoading,
        addCustomerOption,
        addItemCodeOption
    };
};
